package com.takepoints21.mobile;

import java.io.IOException;
import java.util.List;

import com.takepoints.android.TPConst;
import com.takepoints.android.vo.TakeReturn;
import com.takepoints.android.bo.Promotion;
import com.takepoints.android.services.PublicServices;
import com.takepoints.android.util.SecurityUtil;
import com.takepoints.android.util.StringUtil;

import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;

public class TakeIt extends BaseActivity {
	
	@Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.verifycode);
        manageMenu(5);
	}
	
	private void resolveIntent(Intent intent){
		String action = intent.getAction();
		String encryptedCode = "";
		new VerifyCodeProcess(this).execute(encryptedCode);
	}
	
	@Override
	public void onBackPressed(){
		backToHome();
		return;
	}
	
	private void alertResult(String msg){
		final AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
		alertbox.setMessage(msg);
		alertbox.setPositiveButton("OK", new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dlgInf, int which){
				backToHome();
			}
		});
		alertbox.show();
	}
	private void backToHome(){
		Intent intent = new Intent(this, Takepoints.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
    }
	
	class VerifyCodeProcess extends AsyncTask<String, Void, TakeReturn>{
		
		private Context context = null;
		private ProgressDialog progressDlg = null;
		
		VerifyCodeProcess(Context context){
			this.context = context;
		}
		
		@Override
        protected void onPreExecute(){
            super.onPreExecute();
            progressDlg = ProgressDialog.show(context, "Takepoints.com", "Taking ...", true);
		}
		
		@Override
        protected TakeReturn doInBackground(String... params){
			String encryptedCode = params[0];
			TakeReturn tr = new TakeReturn();
			tr.setPromotionSize(-1);
			try{
				String nfcId = SecurityUtil.decrypt(encryptedCode, SecurityUtil.KEY_TYPE_PRIVATE);
				
				List<Promotion> promoL = PublicServices.getSitePromoListByNfcId(nfcId);
				if(promoL != null){
					if(promoL.size() == 1){
						tr = PublicServices.takeNow( getSharedIntValue(TPConst.PRF_MEMBER_ID)
												   , promoL.get(0).getPromotionId()
												   , nfcId );
					}
					tr.setPromotionSize(promoL.size());
				} else{
					tr.setPromotionSize(0);
				}
				tr.setNfcId(nfcId);
			} catch(Exception e){
				tr.setMessage(e.getMessage());
			}
			return tr;
		}
		
		@Override
        protected void onPostExecute(TakeReturn tr){
        	progressDlg.dismiss();
        	if(tr != null && !StringUtil.isEmpty(tr.getTrxCode())){
        		alertResult( "กรุณาแสดงรหัสนี้แก่พนักงาน : " + tr.getTrxCode()
       				 	 + (!StringUtil.isEmpty(tr.getTrxDesc())?"\n\n(" + tr.getTrxDesc() + ")":"") );
        		
			} else if(tr != null && tr.getPromotionSize() > 1){
				goToPromotionList(tr.getNfcId());
				
			} else if(tr != null && !StringUtil.isEmpty(tr.getMessage())){
				alertResult(tr.getMessage());
				
			} else{
				alertResult("ไม่พบโปรโมชั่น ณ ร้านค้านี้ !");
			}
		}
		
	}
	
	private void goToPromotionList(String nfcId){
		Intent intent = new Intent(this, TPPromotions.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.putExtra("nfcId", nfcId);
		startActivity(intent);
    }
	
}
